<?php

namespace App\Admin\Actions\NetworkGoods;

use App\Models\yb\NetworkGoodsModel;
use Encore\Admin\Actions\BatchAction;
use Illuminate\Database\Eloquent\Collection;
use Encore\Admin\Facades\Admin;



class OptionWarehousing extends BatchAction
{

    protected $selector = '.on-shelf';

    public $name = '<a class=\'on-shelf btn btn-sm btn-info\'><i class=\'fa fa-info-circle\'></i>&nbsp;选品入库</a>';

    public function handle(Collection $collection)
    {
        try {
            foreach ($collection as $model) {
                $data = NetworkGoodsModel::where(['network_id'=>admin::user()->id,'goods_id'=>$model->id])->first();
                if(empty($data)){
                    NetworkGoodsModel::insert([
                        'network_id'=>admin::user()->id,
                        'goods_id'=>$model->id,
                        'goods_type'=>$model->goods_type,
                        'created_at'=>date('Y-m-d H:i:s',time())
                    ]);
                }
            }
            return $this->response()->success('入库成功')->refresh();

        } catch (Exception $e) {
            return $this->response()->error('产生错误：'.$e->getMessage());
        }

    }

    public function dialog()
    {
        $this->confirm('您确定入库选中的商品吗？');
    }

}
